package com.soar.tbox.protocol;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.annotation.SuppressLint;

import com.soar.tbox.CommandStorage;
import com.soar.tbox.DisplayHandler;
import com.soar.tbox.Messenger;
import com.soar.tbox.TboxTimer;
import com.soar.tbox.bean.Command;
import com.soar.tbox.bean.Paramter;
import com.soar.tbox.util.Converts;
import com.soar.tbox.util.LogUtil;
import com.soar.tbox.util.SettingHelper;

/**
 * 数据采集上报_车辆状态定时上报指令处理
 * 
 * @author songjunjie
 * 
 */
public class Protocol0803 implements Protocol {
	/**
	 * 指令名. 车辆状态上报
	 */
	private static final String NAME = "0803";

	private Command cmd = null;
	private String serialNum = null;

	public Protocol0803(Command cmd, String serialNum) {
		this.cmd = cmd;
		this.serialNum = serialNum;
	}

	@SuppressLint("SimpleDateFormat")
	@Override
	public void handle() {
		DisplayHandler.show("执行车辆状态定时上报指令");
		int interval = 0;// 时间间隔
		List<Paramter> paramlist1 = cmd.getParamterList();
		for (Paramter param : paramlist1) {
			byte[] key = param.getKey();
			if (key[0] == 0x0a) {// 此参数是时间间隔
				byte[] val = param.getValue();
				interval = Converts.uwordToInt(val);// 单位是秒
				break;
			}
		}

		final int _interval = interval;
		TimerTask task = new TimerTask() {
			@Override
			public void run() {
				Command resultCommand = CommandStorage.getCommand(NAME);
				Messenger messenger = new Messenger();
				try {
					messenger.send(resultCommand, false);
					DisplayHandler
							.show("车辆为状态上报开始执行，每隔" + _interval + "秒，上报一次");
				} catch (Exception e) {
					LogUtil.error("上报车辆状态时出现错误", e);
					e.printStackTrace();
				}
			}
		};
		TboxTimer.schedule("0804", task, 0, interval);
	}
}